﻿using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using DMS.Business.Modules.Statistics;
using DMS.Business.Utility;
using InfoSoftGlobal;

namespace DMS.Web.Modules.Statistics
{
    public partial class StatBaseInfo : System.Web.UI.Page
    {
        #region 属性

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControls();
                Stat();
            }
        }

        private void InitControls()
        {
            //默认显示当前月
            txtBeginInitialTime.Text = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd");

            //List<PS_ProjectTypes> list = new ProjectTypeService().GetTopTypes();
            //ddlProjectType.DataSource = list;
            //ddlProjectType.DataBind();
            //ddlProjectType.Items.Insert(0, "");
        }

        private void Stat()
        {
            //if (ddlProjectType.SelectedIndex == 0)
            //{
            //    ClientHelper.Instance.Alert("请先选择项目类型！");
            //    return;
            //}

            string condition = "";
            if (txtBeginInitialTime.Text != "")
            {
                condition += string.Format(" and RegistDate >= '{0}'", DateTime.Parse(txtBeginInitialTime.Text));
            }
            if (txtEndInitialTime.Text != "")
            {
                condition += string.Format(" and RegistDate < '{0}'", DateTime.Parse(txtEndInitialTime.Text).AddDays(1));
            }
            DataSet ds = null;
            if (this.ddlType.SelectedValue == "1")
            {
                ds = new StatService().StatByPayType(condition);
            }
            else if (this.ddlType.SelectedValue == "2")
            {
                ds = new StatService().StatByType(condition);
            }
            //DataSet ds = new StatService().StatByProjectType(SystemContext.Current.LoginAuthUser, ddlProjectType.SelectedValue, condition);

            gvList.DataSource = ds;
            gvList.DataBind();

            DrawChart(ds);

            //总数量
            decimal totalQuantity = 0;
            if (ds != null)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    totalQuantity += int.Parse(row["subtotal"].ToString());
                }
            }
            lbTotalQuantity.Text = totalQuantity.ToString();

            //总数量
            decimal countQuantity = 0;
            if (ds != null)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    countQuantity += int.Parse(row["totalInsuranceCount"].ToString());
                }
            }
            lbcountQuantity.Text = countQuantity.ToString();

            decimal infoQuantity = 0;
            if (ds != null)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    infoQuantity += int.Parse(row["ArchivesInfoCount"].ToString());
                }
            }
            lbinfoQuantity.Text = infoQuantity.ToString();

            decimal pageQuantity = 0;
            if (ds != null)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    pageQuantity += int.Parse(row["PageCount"].ToString());
                }
            }
            lbpageQuantity.Text = pageQuantity.ToString();

            DJLABEL.Text = new StatService().DengjiCount(condition);
            ZJLABEL.Text = new StatService().ZJCount(condition);
        }

        /// <summary>
        ///  画图
        /// </summary>
        private void DrawChart(DataSet ds)
        {
            string chartTitle = "";
            if (this.ddlType.SelectedValue == "1")
            {
                chartTitle = "缴费单位类型";
            }
            else if (this.ddlType.SelectedValue == "2")
            {
                chartTitle = "单位、个体户、个人";
            }
            StringBuilder xmlData = new StringBuilder();
            xmlData.Append(
                string.Format(
                    "<chart caption='{0}统计' xAxisName='{0}' yAxisName='户数' showValues='1' formatNumberScale='0' showBorder='0'>",
                    chartTitle));
            if (ds != null)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    xmlData.Append(string.Format("<set label='{0}' value='{1}' />", row["name"], row["subtotal"]));
                }

            }
            xmlData.Append("</chart>");
            litChart.Text = FusionCharts.RenderChart("../../FusionCharts/Column3D.swf", "", xmlData.ToString(),
                                                     "myChart", "660", "500", false, true);
        }


        protected void btnStat_Click(object sender, EventArgs e)
        {
            Stat();
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvList.PageIndex = e.NewPageIndex;
            Stat();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        }
    }
}